<template>
  <div class="register-container">
    <div class="register-box">
      <h2>{{ $route.meta.title }}</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label>用户名</label>
          <input type="text" v-model="form.username" required />
        </div>
        <div class="form-group">
          <label>昵称</label>
          <input type="text" v-model="form.nickname" required />
        </div>

        <div class="form-group">
          <label>邮箱</label>
          <input type="email" v-model="form.userEmail" required />
        </div>

        <div class="form-group">
          <label>手机号</label>
          <input type="text" v-model="form.userPhone" required />
        </div>
        <div class="form-group">
          <label>密码</label>
          <input type="password" v-model="form.password" required />
        </div>
        <div class="form-group">
          <label>确认密码</label>
          <input type="password" v-model="form.confirmPassword" required />
        </div>
        <div class="form-group">
          <label>用户类型</label>
          <select v-model="form.userType">
            <option value="User">普通用户</option>
            <option value="Admin">管理员</option>
          </select>
        </div>
        <button type="submit">注册</button>
        <p>
          已有账号? <router-link to="/login">立即登录</router-link>
        </p>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import axios from 'axios'

const router = useRouter()
const userStore = useUserStore()

const form = ref({
  username: '',
  password: '',
  confirmPassword: '',
  userType: 'User'
})

const handleRegister = async () => {
  if (form.value.password !== form.value.confirmPassword) {
    alert('密码与确认密码不一致')
    return
  }

  try {
    const response = await axios.post('/api/register', {
      userName: form.value.username,
      password: form.value.password,
      nickname: form.value.nickname,
      userEmail: form.value.userEmail,
      userPhone: form.value.userPhone,
      role: form.value.userType
    })

    alert(response.data || '注册成功！请登录')
    router.push('/login')
  } catch (error) {
    console.error(error.response); // 在控制台看详细内容
    alert(JSON.stringify(error.response?.data) || '注册失败');
  }
}
</script>


<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.register-box {
  background-color: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 350px;
}

h2 {
  margin-bottom: 20px;
  color: #333;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
}

.radio-group {
  display: flex;
  gap: 15px;
}

.radio-group label {
  display: flex;
  align-items: center;
  gap: 5px;
}

.note {
  color: #666;
  font-size: 12px;
  margin-top: 5px;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #2196F3;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  margin-top: 10px;
}

button:hover {
  background-color: #0b7dda;
}

p {
  margin-top: 15px;
  font-size: 14px;
}

router-link {
  color: #2196F3;
  text-decoration: none;
}

router-link:hover {
  text-decoration: underline;
}

.error-message {
  color: #f44336;
  font-size: 12px;
  margin-top: 5px;
}
</style>